package org.cskj.demo.controller;

import org.cskj.demo.pojo.Shop;
import org.cskj.demo.pojo.User;
import org.cskj.demo.service.imp.ShopImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/shop")
public class ShopController {
    private final double size=8;
    @Autowired
    private ShopImp shopImp;
    @RequestMapping("/get")
    private List<Shop> getall(int page){
        int count =shopImp.count();
        double pageMax=Math.ceil(count/size);
        //计算page是否有效
        page=page>(int)(pageMax)?(int)(pageMax):page;
        //获取数据
        List<Shop> shops=shopImp.seletcLimit(page, (int)size);
        return shops;
    }
    @RequestMapping("/count")
    public int count(){
        int count = shopImp.count();
        return count;
    }
    @RequestMapping("/all")
    public List<Shop> getALL(){
        List<Shop> list = shopImp.getall();
        return list;
    }
    @RequestMapping("/insert")
    public String insert(Shop shop){
        boolean check1=shopImp.checkname(shop.getShopname());
        if(check1){
            int i=shopImp.insert(shop);
            if(i>0){
                return "ok";
            }else {
                return "error";
            }
        }else {
            return "s";
        }
    }
    @RequestMapping("/check")
    public Shop check(String shopname){
        List<Shop> list = shopImp.check(shopname);
        if (list.size()==0){
            System.out.println("----");
            return new Shop();
        }else {
            return list.get(0);
        }
    }
    @RequestMapping("/checkname")
    public String checkName(String shopname){
        boolean checkname =shopImp.checkname(shopname);
        if (checkname){
            return "ok";
        }else {
            return "error";
        }
    }
    @RequestMapping("/update")
    public String update(Shop shop){
        int update = shopImp.update(shop);
        if (update>0){
            return "ok";
        }else {
            return "error";
        }
    }
    @RequestMapping("/delete")
    public String delete(int shopid){
        System.out.println(shopid);
        int delete = shopImp.delete(shopid);

        if (delete>0){
            return "ok";
        }else {
            return "error";
        }
    }
}
